<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('积分兑换商品核销')"/>
</head>
<body class="gray-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <input name="exchangeCode" type="hidden" th:value="${exchangeCode}"/>
        <ul class="sortable-list connectList agile-list" th:object="${info}">
            <li class="warning-element" th:if="*{orderStatus eq 0}">
                <p>商品名称：<span th:text="*{productName}"></span></p>
                <p>商品图片： <img th:src="*{productCoverUrl}" style="max-width: 130px;max-height: 130px;"></p>
                <p>商品数量：1份</span></p>
                <p>兑换积分：<span th:text="*{payCost}"></span></p>
                <p>会员手机：<span th:text="${memberPhone}"></span></p>
                <p>兑换时间：<i class="fa fa-clock-o"></i><span th:text="${createTime}"></span></p>
                <p>核销状态：<i class="fa fa-close"></i></p>
                <p>核销密码：<input type="password" name="password"/></p>
                <p><a href="javascript:void(0)" id="exchange_btn" class="btn btn-primary">确认核销</a></p>
            </li>
            <li class="success-element" th:if="*{orderStatus eq 1}">
                <p>商品名称：<span th:text="*{productName}"></span></p>
                <p>商品图片： <img th:src="*{productCoverUrl}" style="max-width: 200px;max-height: 200px;"></p>
                <p>商品数量：1份</span></p>
                <p>兑换积分：<span th:text="*{payCost}"></span></p>
                <p>会员手机：<span th:text="${memberPhone}"></span></p>
                <p>兑换时间：<i class="fa fa-clock-o"></i><span th:text="${createTime}"></span></p>
                <p>核销状态：<i class="fa fa-check"></i></p>
                <p>核销时间：<i class="fa fa-clock-o"></i><span th:text="${completeTime}"></span></p>
            </li>
        </ul>
    </div>
    <th:block th:include="include :: footer"/>
    <script src="https://cdn.bootcdn.net/ajax/libs/blueimp-md5/2.16.0/js/md5.js"></script>
    <script>
        var isExchange = false;
        var exchangeUrl = ctx + "back/shopOrder/creditExchange/";

        $(function() {
            $('#exchange_btn').click(function () {
                var password = $('input[name="password"]').val();
                var exchangeCode = $('input[name="exchangeCode"]').val();
                if (password === "") {
                    // layer.msg("请输入核销密码！", {icon: 2});
                    // return false;
                    password="empty";
                }
                if (exchangeCode === "") {
                    layer.msg("信息已更新，请刷新页面！", {icon: 2});
                    return false;
                }
                password = md5(password);
                layer.confirm("确认核销该商品？", {
                    icon: 3,
                    title: "提示",
                    btn: ['确认', '取消']
                }, function (index) {
                    if (isExchange) {
                        return false;
                    }
                    isExchange = true;
                    $.ajax({
                        url: exchangeUrl + exchangeCode,
                        data: {"password": password},
                        type: "post",
                        success: function (result) {
                            if (result.code === 0) {
                                layer.msg("核销成功,正在刷新页面……", {
                                    icon: 1,
                                    time: 1500,
                                    shade: [0.1, '#8F8F8F']
                                }, function () {
                                    location.reload();
                                });
                            } else {
                                isExchange = false;
                                layer.msg(result.msg, {icon: 2});
                            }
                        },
                        error: function (res) {
                            isExchange = false;
                            layer.msg("核销超时，请稍后再试！", {icon: 2});
                        }
                    })
                });
            })
        });
    </script>
</body>
</html>
